<template>
  <div class="panel">
    <section>
      <ul>
        <li
          v-for="item in tabs"
          :style="{
            backgroundColor: item.active ? 'white' : '',
            color: item.active ? 'black' : '',
          }"
          @click="tabClick(item)"
        >
          <span>{{ item.name }}</span>
        </li>
      </ul>
    </section>

    <section v-if="tabs[0].active">
      <ul>
        <li v-for="item in searchTypes">
          <p>
            <img :src="item.iconPath" />
            <span>{{ item.nickName }}</span>
          </p>

          <p>
            <el-checkbox v-model="item.checked" size="large" />
          </p>
        </li>
      </ul>
    </section>

    <section v-if="tabs[1].active">
      <el-button
        style="width: 100%; margin-top: 5px"
        type="primary"
        @click="() => (tabs[1].drawerActive = !tabs[1].drawerActive)"
      >
        添加
      </el-button>
    </section>

    <el-drawer
      v-model="tabs[1].drawerActive"
      direction="ltr"
      style="background-color: #dfdfdf"
    >
      <template #header="{ close, titleId, titleClass }">
        <div @click="() => (tabs[1].drawerActive = !tabs[1].drawerActive)">
          <svg-icon size="20" color="black" name="left" />
          <h4 style="color: black">自定义添加</h4>
        </div>

        <div>
          <el-button type="primary" @click="submit">完成</el-button>
        </div>
      </template>

      <div class="drawer-panel">
        <el-input
          v-model="customUrl"
          :rows="2"
          @change="getIcon"
          type="textarea"
          placeholder="搜索引擎地址"
        />
        <el-input :rows="1" type="textarea" placeholder="名称" />

        <ul>
          <li
            v-for="item in drawerTabs"
            :style="{
              backgroundColor: item.active ? 'white' : '',
              color: item.active ? 'black' : '',
            }"
            @click="drawerTabClick(item)"
          >
            <span>{{ item.name }}</span>
          </li>
        </ul>

        <div class="icon-list">
          <img v-for="logo in logos" :src="logo" referrerpolicy="no-referrer" />
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { getStaticResourcePath } from '@/utils/common'
import SvgIcon from '@/components/icon/SvgIcon.vue'
import { getUrlIcons } from '@/api/home'

const tabs = ref([
  { id: '1', name: '默认', active: true, drawerActive: false },
  { id: '2', name: '自定义', active: false, drawerActive: false },
])
const drawerTabs = ref([
  { id: '1', name: '在线图标', active: true },
  { id: '2', name: '本地上传', active: false },
])
const searchTypes = [
  {
    name: 'bing',
    url: `https://cn.bing.com/search?q=`,
    iconPath: 'bing',
    nickName: '必应',
    checked: true,
  },
  {
    name: 'google',
    url: `https://www.google.com/search?q=`,
    iconPath: 'google',
    nickName: '谷歌',
    checked: true,
  },
  {
    name: 'baidu',
    url: `https://www.baidu.com/s?tn=44004473_8_oem_dg&ie=utf-8&wd=`,
    iconPath: 'baidu',
    nickName: '百度',
    checked: true,
  },
]
for (let searchType of searchTypes) {
  searchType.iconPath = getStaticResourcePath(
    `./../../../images/${searchType.iconPath}.webp`,
  )
}

const tabClick = (item) => {
  for (let tab of tabs.value) {
    tab.active = false
  }
  item.active = true
}
const submit = () => {
  tabs.value[1].drawerActive = false
}
let customUrl = ref('')

const drawerTabClick = (item) => {
  for (let el of drawerTabs.value) {
    el.active = false
  }
  item.active = true
}

const logos = ref([])
const getIcon = () => {
  getUrlIcons({ url: 'https://' + customUrl.value }).then((res) => {
    logos.value = logos.value.concat(res.logo)
  })
}
// todo 已有的引擎校验，图标裁剪
</script>

<style scoped lang="less">
.panel {
  width: 100%;
  padding-left: 5px;
  padding-right: 5px;
  background-color: white;
  section:nth-of-type(1) {
    ul {
      display: flex;
      flex-direction: row;
      justify-content: center;
      margin-top: 10px;
      background-color: #c6c6c6;
      border-radius: 2px;
      padding: 2px;
      li {
        margin-left: 10px;
        color: white;
        padding: 2px 5px 2px 5px;
        border-radius: 2px;
      }
      li:hover {
        cursor: pointer;
      }
    }
  }

  section:nth-of-type(2) {
    li {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      p {
        display: flex;
        align-items: center;
      }
    }
    img {
      display: inline-block;
      width: 15px;
      height: 15px;
    }
    span {
      display: inline-block;
      margin-left: 5px;
    }
    label {
    }
  }
}
:deep(.el-drawer__header) {
  display: flex;
  justify-content: space-between;
  align-items: center;
  div:nth-of-type(1) {
    display: flex;
    flex-direction: row;
    align-items: center;
    h4 {
      margin-left: 5px;
    }
  }
  div:nth-of-type(1):hover {
    cursor: pointer;
  }
}
:deep(.el-drawer__close-btn) {
  display: none;
}
.drawer-panel {
  margin-right: 10px;
  :deep(.el-textarea__inner) {
    width: 100%;
    background-color: #c4c3c3;
    resize: none;
    border: none;
    border-radius: 5px;
    padding: 5px;
  }

  ul {
    display: flex;
    flex-direction: row;
    justify-content: center;
    background-color: #ababab;
    width: 100%;
    padding: 5px 0 5px 0;
    border-radius: 2px;
    margin-top: 5px;
    li {
      margin-left: 5px;
      color: white;
      padding: 3px;
      border-radius: 2px;
      cursor: pointer;
    }
  }
  .icon-list {
    img {
      display: inline-block;
      width: 25px;
      height: 25px;
      margin-left: 10px;
      margin-top: 10px;
    }
  }
}
</style>
